package com.wen.gulimall.product.web;

import com.wen.gulimall.product.entity.CategoryEntity;
import com.wen.gulimall.product.service.CategoryService;
import com.wen.gulimall.product.vo.Catelog2Vo;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.redisson.client.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller

public class IndexController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping({"/", "index.html"})
    public String indexPage(Model model) {
        List<CategoryEntity> categoryEntityList = categoryService.getLavel1Categorys();
        model.addAttribute("categorys", categoryEntityList);
        //TODO 1、查询所有的一级分类

        return "index";
    }

    @GetMapping("/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCatalogJson() throws InterruptedException {
        Map<String, List<Catelog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }

    @ResponseBody
    @GetMapping("/hello")


    public String hello() {
        //1、获取一把锁，只要所得名字一样，就是同一吧锁
        RLock lock = redissonClient.getLock("my-lock");
        //2、枷锁
        lock.lock();
        try {
            System.out.println("加锁成功。。。调用业务" + Thread.currentThread().getId());
            Thread.sleep(30000);
        } catch (Exception e) {

        } finally {
            //解锁
            lock.unlock();
            System.out.println("解锁" + Thread.currentThread().getId());
        }
        return "hello";
    }

    @ResponseBody
    @GetMapping("/write")
    public String write() {
        String s = UUID.randomUUID().toString();
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.writeLock();
        rLock.lock();
        try {
            redisTemplate.opsForValue().set("writeValue", s);
            Thread.sleep(30000);
        } catch (Exception e) {

        } finally {
            rLock.unlock();
        }
        return s;

    }

    @ResponseBody
    @GetMapping("/read")
    public String read() {

        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.readLock();
        String writeValue = "";
        rLock.lock();
        try {
            writeValue = (String) redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {

        } finally {
            rLock.unlock();
        }
        System.out.println(writeValue);
        return writeValue;

    }
}
